#!/usr/bin/python3

import os
from smtplib import SMTP
from email.mime.text import MIMEText

repo = os.environ['DRONE_REPO_NAME']
if 'DRONE_TAG' in os.environ:
    ref = os.environ['DRONE_TAG']
else:
    ref = os.environ['DRONE_BRANCH']
bn = os.environ['DRONE_BUILD_NUMBER']

msg = MIMEText(
f'''See {os.environ['DRONE_BUILD_LINK']}

-- 
Hi Developer. Whaaat's happening? Listen. I'm gonna need you to fix
your broken code. So if you could just go ahead and make sure it
builds again, that'd be terrific. OK? Thanks!
''')
msg['From'] = 'Pyrofex Drone <noreply-ci@pyr8.io>'
msg['To'] = os.environ['EMAIL_RECIPIENTS']
msg['Subject'] = f'''Build {bn} of {repo}:{ref} has FAILED'''

with SMTP(os.environ['EMAIL_HOST'], 587) as smtp:
    smtp.starttls()
    smtp.login(os.environ['EMAIL_USERNAME'], os.environ['EMAIL_PASSWORD'])
    smtp.send_message(msg)
    smtp.quit()
